package cn.yusei.teamTribe.service.sign;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;

import cn.yusei.teamTribe.constant.ResponseCode;
import cn.yusei.teamTribe.dao.JedisDAO;

@Service
public class SignService {

    @Autowired
    JedisDAO jedisDAO;

    

    public List<String> getSentence(){
    	   List<String> list = new ArrayList<>();

    	    InputStream ins = null;
    	    BufferedReader reader = null;
    	    try {
    	        Resource resource = new ClassPathResource("sentence.txt");
    	        ins = resource.getInputStream();

    	        reader = new BufferedReader(new InputStreamReader(ins, "UTF-8"));
    	        String line = "";
    	        while ((line = reader.readLine()) != null) {
    	            list.add(line);
    	        }
    	    } catch (Exception e) {
    	        
    	    } finally {
    	        try {
    	            if (reader != null) {
    	                reader.close();
    	                reader = null;
    	            }
    	            if (ins != null) {
    	                ins.close();
    	                ins = null;
    	            }
    	        } catch (IOException e) {
    	            
    	        }
    	    }
    	    return list;
    	}
   
    
    /**
     * 返回今天是否签到
     * @param id
     * @return
     */
    public int SignToday(int id){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Date date = new Date();
        long time = Long.parseLong(sdf.format(date));
        boolean sign = jedisDAO.getbit( id + "_user",time);
        if(sign == true){
            return ResponseCode.ALREADY_HAVE_SIGN;
        }else {
            return ResponseCode.USER_NOT_SIGN;
        }

    }

    /**
     * 获得连续签到天数
     * @param id
     */
    public Integer getSignDay(int id){
    	String key = id + "_user";
        if(jedisDAO.get("last_" + key) == null){
            return null;
        }
        Integer day;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        long total = jedisDAO.bitcout(key);
        Date dateBefore = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dateBefore);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        dateBefore = calendar.getTime();
        long datebefore = Long.parseLong(sdf.format(dateBefore));
        int signToday = SignToday(id);
        boolean flag = jedisDAO.getbit(key,datebefore);
        System.out.println("daybefore :" + datebefore);
        boolean judge = flag || (total == 1 && signToday == 18);
        if(judge){
            day = Integer.parseInt(jedisDAO.get("last_" + key));
        }else {
           day = 0;
           jedisDAO.set("last_" + key,"0");
        }
        return day;
    }


    public int sign(int id){
        String key = id + "_user";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Date date = new Date();
        Date dateBefore = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dateBefore);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        dateBefore = calendar.getTime();
        long datebefore = Long.parseLong(sdf.format(dateBefore));
        boolean today = jedisDAO.getbit(key,Long.parseLong(sdf.format(date)));
        if(today){
            return ResponseCode.ALREADY_HAVE_SIGN;
        }
        boolean flag = jedisDAO.getbit(key,datebefore);
        jedisDAO.setbit(key,Long.parseLong(sdf.format(date)),"1");
        System.out.println("today :" +  date);
        if(flag){
            jedisDAO.incr( "last_" + key);
        }else {
            jedisDAO.set("last_" + key,"1");
        }
        return ResponseCode.REQUEST_SUCCESSED;
    }


}
